﻿using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using LFloatMath.Collision2D;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class TargetSelect_Fan : TargetSelect
    {
        public override ETargetSelectType selectType => ETargetSelectType.Fan;
        
        public override bool selectByUnit => false;
        
        public LFloat raduis;
        public LFloat angle;
        
        protected override void OnInitialize(TargetSelectConfig rConfig)
        {
            base.OnInitialize(rConfig);
            if (rConfig.customConfig is TargetSelect_Fan_ConfigData rData)
            {
                this.raduis = new LFloat(true, rData.raduis);
                this.targetDisRange = new LFloat(true, rData.centerRange);
                this.angle = new LFloat(true, rData.angle);
            }
        }

        protected override void OnInitialize(IReadOnlyList<int> rTabConfig)
        {
            base.OnInitialize(rTabConfig);
            if (rTabConfig.Count > 2)
            {
                this.raduis = new LFloat(true, rTabConfig[0]);
                this.targetDisRange = new LFloat(true, rTabConfig[1]);
                this.angle = new LFloat(true, rTabConfig[2]);
            }
        }

        public override void TrySelect(SelectParam rParam, LVector2 rSelfPos, ref List<Unit> rTargets)
        {
            var rPos = rParam.pos;
            //验证自己与目标的距离，超过了要缩回来
            var rDis = rPos - rSelfPos;
            rDis = LVector2.ClampMagnitude(rDis, this.targetDisRange);
            rPos = rSelfPos + rDis;
            rParam.pos = rPos;
            
            var nDeg = LMath.AngleIntUp(rDis);
            var rProxy = ColliderProxy.Fetch();
            rProxy.Init(CollisionUtil.GetFanPrefab(this.raduis, this.angle, LVector2.up), rPos);
            rProxy.IsStatic = false;
            rProxy.LayerType = 0;
            rProxy.deg = nDeg;
            
            this.CollisionCheck(rProxy,ref rTargets);
            ColliderProxy.Free(rProxy);
        }
        
        public override void Free()
        {
            ClassPool.Put(this);
        }
    }
}